package cn.swing.his.operation.webapi.controller;


import cn.swing.his.common.web.JsonPage;
import cn.swing.his.common.web.JsonResult;
import cn.swing.his.operation.service.IOperationService;
import cn.swing.his.pojo.affair.vo.MedicineDetailsVO;
import cn.swing.his.pojo.operation.dto.OrderSettingDTO;
import cn.swing.his.pojo.operation.vo.*;
import cn.swing.his.pojo.user.vo.MedicalRecordVO;
import cn.swing.his.pojo.operation.dto.PatientCheckItemDTO;
import cn.swing.his.pojo.operation.vo.OrderSettingVO;
import cn.swing.his.pojo.operation.dto.PatientSimpleInfoDTO;
import cn.swing.his.pojo.operation.vo.PatientSimpleInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Map;


@Slf4j
@RestController
@RequestMapping(value = "/operation", produces = "application/json;charset=utf-8")
@Api(tags = "医生操作模块")
@CrossOrigin
public class OperationController {
    @Autowired
    IOperationService operationService;

    /**********************************  insert   *********************************/

    @GetMapping("/insertPrescriptionMedicine")
    @ApiOperation("增加一个处方表-药品购买表")
    public JsonResult<String> insertPrescriptionMedicine(PrescriptionMedicineVO prescriptionMedicineVO) {
        log.info("处方表-药品购买表{}", prescriptionMedicineVO);
        operationService.insertPrescriptionMedicine(prescriptionMedicineVO);
        return JsonResult.ok("添加成功！");
    }

    @GetMapping("/insertPrescription")
    @ApiOperation("增加处方表")
    public JsonResult<String> insertPrescription(PrescriptionVO prescriptionVO) {
        log.info("prescriptionVO:{}",prescriptionVO);
        operationService.insertPrescription(prescriptionVO);
        return JsonResult.ok("添加成功！");
    }


    @PostMapping("/insert-patient-info")
    @ApiOperation("插入患者信息")
    public JsonResult<PatientSimpleInfoVO> insertPatientInfo(@RequestBody PatientSimpleInfoDTO simpleInfoDTO) {
        log.info("simpleInfoDTO:{}", simpleInfoDTO);
        PatientSimpleInfoVO patientSimpleInfoVO = operationService.insertPatientInfo(simpleInfoDTO);
        return JsonResult.ok("添加成功！", patientSimpleInfoVO);
    }

    @PostMapping("/insertCheckForPatient")
    @ApiOperation("为患者增加检查项")
    public JsonResult<String> insertCheckForPatient(PatientCheckItemDTO patientCheckItemDTO) {
        operationService.insertCheckForPatient(patientCheckItemDTO);
        return JsonResult.ok("添加成功！");
    }

    @PostMapping("/insert-order-setting")
    @ApiOperation("插入预约信息")
    public JsonResult<Void> insertOrderSetting(@RequestBody OrderSettingDTO orderSettingDTO){
        log.info("orderSettingDTO:{}", orderSettingDTO);
        int i = operationService.insertOrderSetting(orderSettingDTO);
        return JsonResult.ok("添加成功！");
    }




    /**********************************  delete   *********************************/
    @GetMapping("/delete_order_setting_by_id")
    @ApiOperation("通过id删除预约信息")
    public JsonResult<String> deleteOrderSettingById(Integer id) {
        log.info("id:{}",id);
        operationService.deleteOrderSettingById(id);
        return JsonResult.ok("删除成功！");
    }

    @GetMapping("/delete-patient-info-by-id")
    @ApiOperation("修删除用户信息通过id")
    public JsonResult<Integer> deletePatientInfoById(Integer id) {
        log.info("id = {}", id);
        int i = operationService.deletePatientInfoById(id);
        return JsonResult.ok("删除成功", i);
    }


    /**********************************  update   *********************************/

    @GetMapping("/updatePrescriptionState")
    @ApiOperation("通过处方单id修改用户支付状态")
    public JsonResult<String> updatePrescriptionState(Integer id) {
        operationService.updatePrescriptionStateById(id);
        return JsonResult.ok("修改成功！");
    }

    @PostMapping("/update-patient-info")
    @ApiOperation("修改用户信息")
    public JsonResult<PatientSimpleInfoVO> updatePatientInfoByIdCard(@RequestBody PatientSimpleInfoDTO simpleInfoDTO) {
        log.info("simpleInfoDTO = {}", simpleInfoDTO);
        PatientSimpleInfoVO simpleInfoVO = operationService.updatePatientInfoByIdCard(simpleInfoDTO);
        log.info("simpleInfoVO = {}", simpleInfoVO);
        return JsonResult.ok("查询成功", simpleInfoVO);
    }

    @GetMapping("/update-patient-state-seeing")
    @ApiOperation("修改用户信息就诊中")
    public JsonResult<Integer> updatePatientStateByOrderSettingId(Integer orderSettingId) {
        log.info("orderSettingId = {}", orderSettingId);
        int i = operationService.updateOrderSettingStateToSeeing(orderSettingId);
        return JsonResult.ok("操作成功", i);
    }


    @PostMapping("/update-patient-info-by-id")
    @ApiOperation("修改用户信息通过id")
    public JsonResult<Integer> updatePatientInfoById(@RequestBody PatientInfoVO patientInfoVO) {
        log.info("patientInfoVO = {}", patientInfoVO);
        int i = operationService.updatePatientInfoById(patientInfoVO);
        return JsonResult.ok("修改成功", i);
    }

    /**********************************  select   *********************************/

    @PostMapping("/get-orders")
    @ApiOperation("通过医生id查看预约信息")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "医生id", name = "doctorId", example = "3"),
            @ApiImplicitParam(value = "页码", name = "pageNum", example = "1"),
            @ApiImplicitParam(value = "每页数量", name = "pageSize", example = "10"),
            @ApiImplicitParam(value = "患者id", name = "patientId", example = ""),
            @ApiImplicitParam(value = "状态", name = "state", example = "")
    })
    public JsonResult<JsonPage<OrderSettingVO>> getOrderByDoctorId(@RequestBody SearchOrderVO searchOrderVO) {
        log.info("searchOrderVO = {}", searchOrderVO);
        JsonPage<OrderSettingVO> jsonPage = operationService.getOrderSettingByDoctorId(searchOrderVO);
        return JsonResult.ok("查询成功", jsonPage);
    }



    @GetMapping("/get-medical-record")
    @ApiOperation("通过用户id查询病历单")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页码", name = "pageNum", example = "1"),
            @ApiImplicitParam(value = "每页条数", name = "pageSize", example = "1"),
            @ApiImplicitParam(value = "用户id", name = "idCard", example = "320611197004126208"),
    })
    public JsonResult<JsonPage<MedicalRecordVO>> getMedicalRecordByUserIdCard(Integer pageNum, Integer pageSize, String idCard){
        JsonPage<MedicalRecordVO> jsonPage = operationService.getMedicalRecordByIdCard(pageNum, pageSize, idCard);
        return JsonResult.ok("查询成功", jsonPage);
    }

    @GetMapping("/get-patient-info")
    @ApiOperation("通过就诊卡号查询患者信息")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "患者id", name = "id", example = "1")
    })
    public JsonResult<PatientSimpleInfoVO> getPatientInfoByPatientId(Integer id){
        log.info("patientId:{}", id);
        PatientSimpleInfoVO patientInfoByPatientId = operationService.getPatientInfoByPatientId(id);

        return JsonResult.ok("查询成功",patientInfoByPatientId);
    }

    @PostMapping("/judge-patient-info")
    @ApiOperation("根据患者身份证判断是否存在该用户")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "患者身份证", name = "idCard", example = "150424198402058590")
    })
    public JsonResult<Void> getPatientInfoByIdCard(String idCard){
        log.info("idCard:{}", idCard);
        operationService.getPatientInfoByIdCard(idCard);
        return JsonResult.ok("该用户不存在");
    }

    @GetMapping("/get_room_list")
    @ApiOperation("获取科室信息")
    public JsonResult<List<RoomVO>> getRoomList(){
        List<RoomVO> roomSimpleInfo = operationService.getRoomSimpleInfo();
        log.info("roomSimpleInfo:{}", roomSimpleInfo);
        return JsonResult.ok("查询成功",roomSimpleInfo);
    }

    @GetMapping("/get_doctor_list_by_room_id")
    @ApiOperation("获取医生信息通过科室id")
    public JsonResult<List<DoctorSimpleVO>> getDoctorListByRoomId(Integer roomId){
        log.info("roomId:{}", roomId);
        List<DoctorSimpleVO> doctorListByRoomId = operationService.getDoctorListByRoomId(roomId);
        log.info("doctorListByRoomId:{}", doctorListByRoomId);
        return JsonResult.ok("查询成功",doctorListByRoomId);
    }


    @GetMapping("/getPatientSickRecord")
    @ApiOperation("通过就诊卡号查询患者历史患病信息")
    public JsonResult<List<PersonalSickRecordVO>> getPatientSickRecordById(Integer id) {
        List<PersonalSickRecordVO> patientSickRecord = operationService.getPatientSickRecordById(id);
        return JsonResult.ok("查询成功", patientSickRecord);
    }

    @GetMapping("/get_order_setting_count")
    @ApiOperation("获取今日预约人数")
    public JsonResult<Integer> getOrderSettingCount(){
       return JsonResult.ok("查询成功",operationService.getOrderSettingCount());
    }

    @GetMapping("/get_finish_order_setting_count")
    @ApiOperation("获取今日就诊人数")
    public JsonResult<Integer> getFinishOrderSettingCount(){
        return JsonResult.ok("查询成功",operationService.getFinishOrderSettingCount());
    }

    @GetMapping("/get_order_setting_service_count")
    @ApiOperation("获取各门诊近一个月的数据")
    public JsonResult<List<OrderSettingServiceVO>> getOrderSettingServiceCount(){
        return JsonResult.ok("查询成功",operationService.getOrderSettingServiceCount());
    }

    @GetMapping("/get_count_of_nearly_seven_day_or_yesterday")
    @ApiOperation("获取经七天或昨天的数据")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "天数", name = "time", example = "1")
    })
    public JsonResult<List<Map<String, String>>> getCountOfNearlySevenDayOrYesterday(Integer time){
        log.info("controller:{}",time);
        return JsonResult.ok("查询成功",operationService.getCountOfNearlySevenDayOrYesterday(time));
    }


    @GetMapping("/get_medicine_that_will_expire")
    @ApiOperation("获取将要过期的药品")
    public JsonResult<List<MedicineSimpleVO>> getMedicineThatWillExpire(){
        return JsonResult.ok("查询成功",operationService.getMedicineThatWillExpire());
    }

    @GetMapping("/get_medicine_that_less_numbers")
    @ApiOperation("获取库存少于30的药")
    public JsonResult<List<MedicineSimpleVO>> getMedicineThatLessNumbers(){
        return JsonResult.ok("查询成功",operationService.getMedicineThatLessNumbers());
    }


    @GetMapping("/get_doctor_detail_by_room_id")
    @ApiOperation("根据科室id查询所有医生信息")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "科室id", name = "roomId", example = "1")
    })
    public JsonResult<List<DoctorDetailVo>> getDoctorDetailByRoomId(Integer roomId){
        return JsonResult.ok("查询成功",operationService.getDoctorDetailByRoomId(roomId));
    }

    @PostMapping("/get_patient_info_by_telephone")
    @ApiOperation("根据电话号码查询患者信息")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "电话", name = "number", example = "15363541052")
    })
    public JsonResult<List<PatientInfoVO>> getPatientByTelephone(String number){
        return JsonResult.ok("查询成功",operationService.getPatientByTelephone(number));
    }


    @PostMapping("/get_order_setting_by_telephone")
    @ApiOperation("根据电话号码查询患者预约信息")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "电话", name = "telephone", example = "18995272089")
    })
    public JsonResult<List<OrderSettingVO>> getOrderSettingVOByTelephone(String telephone){
        return JsonResult.ok("查询成功",operationService.getOrderSettingVOByTelephone(telephone));
    }


    @PostMapping("/get_medical_history_by_telephone")
    @ApiOperation("根据电话号码查询患者病史")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "电话", name = "telephone", example = "15363541052")
    })
    public JsonResult<List<MedicalHistoryVO>> getMedicalHistoryByTelephone(String telephone){
        return JsonResult.ok("查询成功",operationService.getMedicalHistoryByTelephone(telephone));
    }

    @PostMapping("/get_medical_report_by_telephone")
    @ApiOperation("根据电话号码查询患者就诊报告")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "电话", name = "telephone", example = "13725571512")
    })
    public JsonResult<List<MedicalReportVO>> getMedicalReportByTelephone(String telephone){
        return JsonResult.ok("查询成功",operationService.getMedicalReportByTelephone(telephone));
    }


    @PostMapping("/get_patient_info_by_id")
    @ApiOperation("根据id查询患者信息")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "患者id", name = "patientId", example = "1")
    })
    public JsonResult<PatientVO> getPatientInfoById(Integer patientId){
        log.info("patientId:{}",patientId);
        return JsonResult.ok("查询成功",operationService.getPatientInfoById(patientId));
    }


    @GetMapping("/getMedicineList")
    @ApiOperation("查询所有药品信息")
    public JsonResult<List<MedicineDetailsVO>> getMedicineList(){
        List<MedicineDetailsVO> medicineList = operationService.getMedicineList();
        log.info("medicineList = " + medicineList);
        return JsonResult.ok("查询成功",medicineList);
    }

    @GetMapping("/getMedicineListByName")
    @ApiOperation("根据关键字查询药品信息")
    public JsonResult<List<MedicineDetailsVO>> getMedicineListByName(String name){
        List<MedicineDetailsVO> medicineListByName = operationService.getMedicineListByName(name);
        log.info("符合条件的药品有{}",medicineListByName);
        return JsonResult.ok("查询成功",medicineListByName);
    }
}
